<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/5
 * Time: 17:42
 */

namespace App\Game\Traits;

use App\Models\Inf\Player;

trait OgTrait
{

    /**
     * gameapi
     * 2018/9/24 13:55
     * Administrator
     * agRequest
     * 封装ag得请求
     * @param $action
     * @return string
     */
    public function agRequest($action)
    {
        //去掉用户名前缀
        if (isset($action['username'])){
            $action['username'] = str_replace($this->pre,'',$action['username']);
        }
        ksort($action,SORT_NATURAL);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'oooo...'.json_encode($action).PHP_EOL, FILE_APPEND);

        $action=json_encode($action);
        $action=openssl_encrypt($action,'AES-128-CBC',$this->appKey,OPENSSL_RAW_DATA ,substr($this->appKey,0,16));

        $post=[];
        $post['appid']=$this->appId;
        $post['ver']='v1';
        $post['params']=urlencode(base64_encode($action));
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ppp..'.json_encode($post).PHP_EOL, FILE_APPEND);
        $result=$this->curlApiInfo($this->gameUrl,$post);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ppp..'.json_encode($result).PHP_EOL, FILE_APPEND);
        return $result;
    }

    /**
     * gameapi
     * 2018/9/24 15:28
     * Administrator
     * prepareTransfer
     * 预备转账
     * @param $accountUserName
     * @param $type
     * @param $actype
     * @param $price
     * @param $transferid
     * @return array
     */
    public function prepareTransfer($accountUserName, $type, $actype, $price, $transferid,$mainGamePlatId)
    {

        $player = Player::where('username', $accountUserName)->where('main_game_plats_id', $mainGamePlatId)->first();
        $action = [
            'platform' => 'OG',
            'method' => $type,
            'username' => $accountUserName,
            'password' => $player->password,
            'orderid' => $this->cagent . $transferid,
            'amount' => $price,
            'currency' => 'CNY',
        ];

        $output = $this->agRequest($action);

        if ($output['status']==0) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '预备转账失败，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '预备转账失败，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);
        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '预备转账失败，错误原因：'.$data['message'].'...'.$accountUserName];
        }
        return ['success' => true, 'data' => ['transferid' => $this->cagent . $transferid, 'transfer_state' => 1], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/24 15:28
     * Administrator
     * transferConfirm
     * 确认转账
     * @param $accountUserName
     * @param $type
     * @param $actype
     * @param $price
     * @param $transferid
     * @return array
     */
    public function transferConfirm($accountUserName, $type, $actype, $price, $transferid,$mainGamePlatId)
    {
        $player = Player::where('username', $accountUserName)->where('main_game_plats_id', $mainGamePlatId)->first();
        $action = [
            'platform' => 'OG',
            'method' => $type,
            'username' => $accountUserName,
            'password' => $player->password,
            'orderid' => $this->cagent . $transferid,
//            'amount' => round($price, 2),
            'currency' => 'CNY',
        ];

        $output = $this->agRequest($action);

        if ($output['status']==0) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '确认转账失败，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '确认转账失败，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);
        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '确认转账失败，错误原因：'.$data['message'].'...'.$accountUserName];
        }
        return ['success' => true, 'data' => [], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/24 17:35
     * Administrator
     * apiDataParse
     * 字段解析
     * @param array $apiData
     * @param array $data
     * @return array
     */
//    public function apiDataParse(array $apiData, array $data)
//    {
//        $data = $this->statusParse($apiData, $data);
//        return $data;
//    }

    /**
     * gameapi
     * 2018/9/24 17:36
     * Administrator
     * statusParse
     * 解析状态字段
     * @param array $apiData
     * @param array $data
     * @return array
     */
//    private function statusParse(array $apiData, array $data)
//    {
//        if (array_key_exists('standard_settle', $apiData)) {
//            switch ($apiData['standard_settle']) {
//                case '1':
//                    $data['status'] = 1;
//                    break;
//                case '0':
//                    $data['status'] = 0;
//                    break;
//                default:
//                    $data['status'] = -1;
//                    break;
//            }
//        }
//        return $data;
//    }
}